package com.example.ass7.animation;

import biuoop.DrawSurface;
import biuoop.KeyboardSensor;

/**
 * @author Meirav Ben Izhak
 */
public class KeyPressStoppableAnimation implements Animation {
    private boolean stop;
    private final Animation animation;
    private final String key;
    private final KeyboardSensor sensor;
    private boolean isAlreadyPressed;

    /**
     * @param sensor
     *            sensor
     * @param key
     *            key
     * @param animation
     *            animation
     */
    public KeyPressStoppableAnimation(
        KeyboardSensor sensor, String key, Animation animation) {

        this.sensor = sensor;
        this.key = key;
        this.animation = animation;
        this.isAlreadyPressed = Boolean.TRUE;
    }

    @Override
    public void doOneFrame(DrawSurface d,
            double dt) {
        this.animation.doOneFrame(d, dt);
        if (this.sensor.isPressed(this.key)) {
            if (!this.isAlreadyPressed) {
                this.stop = true;
            }
        } else {
            this.isAlreadyPressed = Boolean.FALSE;
        }
    }

    @Override
    public boolean shouldStop() {
        if (this.stop) {
            this.stop = Boolean.FALSE;
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}